<template>
	<view class="broadcastPage">
		<!-- <view class="tit">主播还没准备好!</view> -->
		<!-- <view @tap="openBluetoothAdapter">开启蓝牙设备</view> -->
		<!-- 订阅消息 -->
		<view class="" @tap="subscribeMessage">订阅消息</view>
		<!-- 发送模板消息 -->
		<view @tap="send">发送模板消息</view>
	</view>
</template>

<script>
	import Vuex from "vuex";
	export default {
		computed: {
			...Vuex.mapState(["openid"])
		},
		data() {
			return {

			};
		},
		mounted() {
			this.openBluetoothAdapter();
		},
		methods: {
			openBluetoothAdapter() {
				uni.openBluetoothAdapter({
					success: res => {
						if (res.errMsg !== "openBluetoothAdapter:ok") return;
						uni.startBluetoothDevicesDiscovery({
							services: ["xxx"],
							allowDuplicatesKey: false,
							interval: 2000,
							success: res => {
								console.log(res);
							}
						})
					}
				});
			},
			// 订阅消息成功
			subscribeMessage() {
				uni.requestSubscribeMessage({
					tmplIds: ['IDZQJNh6Ezk9w-ry2YnEyq3tS4CjTyREX_MJdpHwnzM'],
					success: res => {
						console.log(res);
						if (res["IDZQJNh6Ezk9w-ry2YnEyq3tS4CjTyREX_MJdpHwnzM"] === "accept") {
							uni.showToast({
								title: `订阅成功`,
								icon: "success"
							});
						};
					}
				});
			},
			// 发送模板消息
			async send() {
				let appid = "wx6ce21f832fe18c70";
				let secret = "09d5922babfa990a2c7b25b47581722b";
				let token = await this.$myrequest({
					url: `cgi-bin/token?grant_type=client_credential&appid=${appid}&secret=${secret}`,
				});
				let res = await this.$myrequest({
					method: "POST",
					url: "cgi-bin/message/wxopen/template/send?access_token=ACCESS_TOKEN",
					data: {
						"access_token": token.data.access_token,
						"touser": this.openid,
						"template_id": "IDZQJNh6Ezk9w-ry2YnEyq3tS4CjTyREX_MJdpHwnzM",
						// "page": "/pages/index/index",
						"form_id": "FORMID",
						"data": {
							"keyword1": {
								"value": "cgctm6"
							},
							"keyword2": {
								"value": "2015年01月05日 12:30"
							}
						},
						// "scene_type": 1,
						// "emphasis_keyword": "keyword1.DATA"
					},
				});
				// 请求的结果
				console.log(res);
			},
		}
	}
</script>

<style lang="scss">
	.broadcastPage {
		display: flex;
	}

	.tit {
		flex: 1;
		color: #b50e03;
		font-size: 50rpx;
		text-align: center;
		margin-top: 50%;
		letter-spacing: 12rpx;
	}
</style>
